import { defineStore } from 'pinia';
import { ref } from 'vue';
import { type User } from '@/types/base';

export const STORE_KEY_SETUP_STORE = 'setup-store';

export const useSetupStore = defineStore(STORE_KEY_SETUP_STORE, () => {
  const recordField = ref<Record<string, string>>({});
  const initializedRecordField = ref<Record<string, string>>({ '1': '_init1', '2': '_init2' });
  const recordUserField = ref<Record<string, User>>({ '1': { name: '1Name', age: 1 } });

  const setHashValue = (key: string, value: string) => {
    recordField.value[key] = value;
  };
  const setHashValues = (...datas: Array<{ key: string; value: string }>) => {
    datas.forEach(({ key, value }) => {
      setHashValue(key, value);
    });
  };

  const setInitHashValue = (key: string, value: string) => {
    initializedRecordField.value[key] = value;
  };
  const setInitHashValues = (...datas: Array<{ key: string; value: string }>) => {
    datas.forEach(({ key, value }) => {
      setInitHashValue(key, value);
    });
  };

  const setUserAge = (key: string, age: number) => {
    recordUserField.value[key].age = age;
  };
  const addUser = (key: string, user: User) => {
    recordUserField.value[key] = user;
  };

  return {
    recordField,
    initializedRecordField,
    recordUserField,
    setHashValue,
    setHashValues,
    setInitHashValue,
    setInitHashValues,
    setUserAge,
    addUser,
  };
});

export type SetupStore = ReturnType<typeof useSetupStore>;
